package com.project.frba.bi.transformation;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import com.project.frba.bi.descriptor.Property;

public class DimensionRow implements Row{
	
	private Long id = 0L;
	private Map<String, Object> propertyColumns = new HashMap<String, Object>();
	private DimensionTable dimensionTable;
	
	public DimensionRow(DimensionTable table){
		this.dimensionTable = table;
		table.addRow(this);
	}
		
	public DimensionTable getDimensionTable() {
		return dimensionTable;
	}

	public void setPropertyColumns(Map<String, Object> propertyColumns) {
		this.propertyColumns = propertyColumns;
	}

	public Long getId(){
		return this.id;
	}

	public void setId(Long id){
		this.id = id;
	}
	
	public String getInsertValueStatement(){
		Collection<Property> properties = this.dimensionTable.getDimension().getProperties();
		StringBuffer value = new StringBuffer();
		value.append("(");
		value.append(this.id);
		for (Property property: properties) {
			value.append(",");
			if(property.getType().equals(String.class)){
				value.append("'" + this.propertyColumns.get(property.getName()) + "'");
			} else {
				value.append(this.propertyColumns.get(property.getName()));	
			}
			
		}
		value.append(")");
		
		return value.toString();
	}
	
	public void addColumnValue(String columnName, Object value){
		propertyColumns.put(columnName, value);
	}
	
	public Map<String,Object> getPropertyColumns(){
		return propertyColumns;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof DimensionRow) {
			DimensionRow valueObj = (DimensionRow) obj;
			for (String fieldKey : this.getPropertyColumns().keySet()) {
				if(!valueObj.getPropertyColumns().containsKey(fieldKey) || !valueObj.getPropertyColumns().get(fieldKey).equals(this.getPropertyColumns().get(fieldKey))){
					return false;
				}
			}
		} else {
			return false;
		}
		return true;
	}
	
	/*
	@Override
	public int hashCode() {
		StringBuffer buffer = new StringBuffer();
		for(String fieldKey: this.getPropertyColumns().keySet()){
			buffer.append(this.getPropertyColumns().get(fieldKey));
		}
		
		return buffer.toString().hashCode();
	}*/
	
}
